package com.chaoxuan.uebt.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.chaoxuan.uebt.R;
import com.chaoxuan.uebt.model.Vercode;
import com.chaoxuan.uebt.utils.BaseSubscriber;
import com.chaoxuan.uebt.utils.CountDownTimerUtils;
import com.chaoxuan.uebt.utils.Loading;
import com.chaoxuan.uebt.utils.RetrofitUtils;
import com.chaoxuan.uebt.utils.ToastUtils;

public class ForgetActivity extends BaseActivity implements View.OnClickListener,TextWatcher {

    EditText editPhone,editVercode,editPassword,editRepassword;
    Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget);
        editPhone=(EditText)findViewById(R.id.edit_phone);
        editPassword=(EditText)findViewById(R.id.edit_password);
        editRepassword=(EditText)findViewById(R.id.edit_repassword);
        editVercode=(EditText)findViewById(R.id.edit_vercode);
        button=(Button) findViewById(R.id.btn_confirm);
        button.setOnClickListener(this);
        button.setSelected(true);
        editPhone.addTextChangedListener(this);
        editPassword.addTextChangedListener(this);
        editVercode.addTextChangedListener(this);
        editRepassword.addTextChangedListener(this);
        findViewById(R.id.btn_vercode).setOnClickListener(this);
        findViewById(R.id.btn_confirm).setOnClickListener(this);
        initTitle();
    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void afterTextChanged(Editable editable) {
        String password=editPassword.getText().toString();
        String repassword=editRepassword.getText().toString();
        String captcha=editVercode.getText().toString();
        if(TextUtils.isEmpty(password)||TextUtils.isEmpty(captcha)||(!password.equals(repassword))){
            button.setSelected(true);
        }else{
            button.setSelected(false);
        }
    }

    @Override
    public void onClick(View v) {
        String phone=editPhone.getText().toString();
        switch (v.getId()){
            case R.id.btn_vercode:
                if(TextUtils.isEmpty(phone))return;
                CountDownTimerUtils mCountDownTimerUtils = new CountDownTimerUtils((Button)v, 60000, 1000); //倒计时1分钟
                mCountDownTimerUtils.start();
                subscribe(RetrofitUtils.getAPIService().sendVercode(phone),new BaseSubscriber<Vercode>(){
                    @Override
                    public void onSuccess(Vercode vercode) {
                        Log.d("aaa","ddd");
                    }
                });
                break;
            case R.id.btn_confirm:
                String password=editPassword.getText().toString();
                String repassword=editRepassword.getText().toString();
                String captcha=editVercode.getText().toString();
                if(TextUtils.isEmpty(password)||TextUtils.isEmpty(captcha)||(!password.equals(repassword)))return;
                final Loading loading=new Loading(this,R.style.dialog);
                loading.show();
                subscribe(RetrofitUtils.getAPIService().findPass(phone,captcha,password),new BaseSubscriber<Object>(){
                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                        loading.dismiss();
                    }
                    @Override
                    public void onSuccess(Object obj) {
                        ToastUtils.showCustomToast("设置成功");
                        startActivity(new Intent(getBaseContext(),LoginActivity.class));
                    }
                });
                break;
        }
    }
}
